package com.baitu.redisdemo.controller;


import com.baitu.redisdemo.entity.User;
import com.baitu.redisdemo.util.RedisOperator;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {

    @Autowired
    public Gson gson;
    @Autowired
    public RedisOperator redisOperator;

    @RequestMapping("/insert")
    public boolean insert(String key ,User user){
        System.out.println(key);
        System.out.println(user);
        String userjson=gson.toJson(user);
        System.out.println(userjson);
        redisOperator.set("json:user",userjson);
        return true;
    }

    @RequestMapping("/select")
    public User select(String key){
        System.out.println(key);
        String userjson= redisOperator.get(key);
        User re=gson.fromJson(userjson,User.class);
        System.out.println(re);
        return re;
    }
}
